<?php
namespace app\admin\model;
use app\admin\builder\Builder;
use think\facade\Db;

class MyBuder
{
    protected $data = [];
    protected $field = [];
    protected $paichu = [];
    protected $builder = '';
    protected $cid = '';
    public function __construct($type='form')
    {
        if($type=='form'){
            $this->builder = Builder::form();
        }else{
            $this->builder = Builder::table();
        }
//        $this->builder = Builder::form();
        return $this;
    }
    static public function form(){
        $obj = new MyBuder('form');
        return $obj;
    }
    static public function table(){
        $obj = new MyBuder('table');
        return $obj;
    }

    /**添加表单跳转地
     * @param $str
     * @return $this
     */
    public function addFormAction($str,$cid=''){
        $this->builder->setAction($str);
        $this->cid = $cid;
        return $this;
    }

    /**添加按钮
     * @param array $array
     * @return $this
     */
    public function addFormButton($array=[]){
        foreach ($array as $value){
            $this->builder->setButton($value['type'],$value['desc']??null,$value['action']??null,$value['class']??null,$value['style']??null);
        }
        return $this;
    }

    /**添加form字段和不显示字段
     * @param array $field
     * @param array $paichu
     * @return $this
     */
    public function addField($field=[],$paichu=[]){
        $this->field = $field;
        $this->paichu = $paichu;
        return $this;
    }

    /**添加form的数据
     * @param array $data
     * @return $this
     */
    public function addData($data=[]){
        $this->data = $data;
        return $this;
    }

    /**加载form表单
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function FormFetch(){
        if($this->data['id']??null){
            $this->builder = $this->builder->setProject('none','id','id',$this->data['id']);
        }
        foreach ($this->field as $value){
            if($value['field']=='cid'){
                $this->builder = $this->builder->setProject('none','cid','栏目',$this->cid);
                continue;
            }
            if(!$value['edit']||in_array($value['field'],$this->paichu)){
                if($this->data){
                    $this->builder = $this->builder->setProject('none',$value['field'],$value['field_name'],$this->data[$value['field']]);
                }else{
                    $this->builder = $this->builder->setProject('none',$value['field'],$value['field_name'],$value['defaults']);
                }
                continue;
            }
            if($value['form_type']=='单行文本'){
                if($this->data){
                    $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='无限联动框'){
                $table = Db::name(getTable('models'))
                    ->where('id',$value['table'])
                    ->find();
                if($value['hierarchy']){
                    $pid =  $value['hierarchy'];
                }else{
                    $pid = 'pid';
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('inclass',$value['field'],$value['field_name'],$this->data[$value['field']],$pid,$value['descs'],'',request()->controller().'/getchild?table='.$table['table']);
                }else{
                    $this->builder = $this->builder->setProject('inclass',$value['field'],$value['field_name'],$value['defaults'],$pid,$value['descs'],'',request()->controller().'/getchild?table='.$table['table']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($this->data){
                    $this->builder = $this->builder->setProject('textarea',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单文件'){
                if($this->data){
                    $this->builder = $this->builder->setProject('file',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='多文件'){
                if($this->data){
                    $this->builder = $this->builder->setProject('files',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单图片'){
                if($this->data){
                    $this->builder = $this->builder->setProject('img',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='多图片'){
                if($this->data){
                    $this->builder = $this->builder->setProject('imgs',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='视频'){
                if($this->data){
                    $this->builder = $this->builder->setProject('video',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单选框'){
                $array = explode(',',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='多选框'){
                $array = explode(',',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='下拉框'){
                $array = explode(',',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='联动单选框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                $array = [];
                if($mytable){
                    $array = Db::name($mytable['table'])->where('status',1)->field()->select()->toArray();
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动多选框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                $array = [];
                if($mytable){
                    if($mytable==getTable('models')){
                        $array = Db::name($mytable['table'])->where('status',1)->where('type','<>',1)->select()->toArray();
                    }else{
                        $array = Db::name($mytable['table'])->where('status',1)->select()->toArray();
                    }
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动下拉框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                $array = [];
                if($mytable){
                    $array = Db::name($mytable['table'])->where('status',1)->select()->toArray();
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动下拉组'){
                $tablesid =  explode(',',$value['table']);
                $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                if($tablesid[1]??null){
                    $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                }else{
                    echo '联动下拉组最少联两表';
                }
                $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                foreach ($array as $key=>$va){
                    $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                    $array[$key] = $va;
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('optgroup',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='编辑框'){
                if($this->data){
                    $this->builder = $this->builder->setProject('editor',$value['field'],$value['field_name'],$this->data[$value['field']],[],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='编辑框(layui)'){
                if($this->data){
                    $this->builder = $this->builder->setProject('editors',$value['field'],$value['field_name'],$this->data[$value['field']],[],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='开关'){
                if($this->data){
                    $this->builder = $this->builder->setProject('switch',$value['field'],$value['field_name'],$this->data[$value['field']],['开启','关闭'],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                }
            }
            if($value['form_type']=='时间'){
                if($this->data){
                    $this->builder = $this->builder->setProject('time',$value['field'],$value['field_name'],$this->data[$value['field']],['开启','关闭'],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                }
            }
            if($value['form_type']=='json数组'){
                $array = explode(':',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('json',$value['field'],$value['field_name'],json_decode($this->data[$value['field']],true),$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                }
            }
        }
        return $this->builder->fetch();
    }

    /**静态获取数据
     * @param array $field
     * @param array $data
     * @return array
     */
    public static function getData($field=[],$data=[]){
        $newdata = [];
        if($data['id']??null){
            $newdata['id'] = $data['id'];
        }
        foreach ($field as $value){
            if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='编辑框(layui)'||$value['form_type']=='单图片'||$value['form_type']=='视频'||$value['form_type']=='多图片'||$value['form_type']=='单文件'||$value['form_type']=='多文件'||$value['form_type']=='联动下拉框'||$value['form_type']=='联动下拉组'){
                if(isset($data[$value['field']])&&($data[$value['field']]||$data[$value['field']]==0)){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='时间'){
//                echo $value['field'].':'.$data[$value['field']];
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = strtotime($data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = 1;
                }else{
                    $newdata[$value['field']] = 0;
                }
            }
            if($value['form_type']=='单选框'||$value['form_type']=='联动单选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'||$value['form_type']=='联动多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='json数组'){
                $options = explode(':',$value['option']);
                $shujuus = $data[$value['field']];
                $newshuju = [];
                foreach ($shujuus[$options[0]] as $key=>$v){
                    $newshuju[$v] = $shujuus[$options[1]][$key];
                }
                $newdata[$value['field']] = json_encode($newshuju,JSON_UNESCAPED_UNICODE);
            }
        }
        return $newdata;
    }



    //table


    /**设置开启分页
     * @param $str
     * @param bool $open
     */
    public function setTablePage($str,$open=true){
        if(!$str){
            $str = request()->controller().'/page';
        }
        $this->builder = $this->builder->setAction(url($str));
        $this->builder = $this->builder->setPage($open);
        return $this;
    }
    /**添加头部
     * @param string $toops
     * @param array $field
     * @param string $cid
     * @param int $width
     * @param string $text
     * @return $this
     */
    public function setTableToops($toops='',$field=[],$cid='',$width=200,$text=''){
        $toops = explode(',',$toops);
        $fieldsousuo = [];
        $timesousuo = [];
        foreach ($field as $value){
            if($value['search']){
                if($value['form_type']=='时间'){
                    $timesousuo[] = $value;
                }else{
                    $fieldsousuo[] = $value;
                }
            }
        }
        $this->builder = $this->builder
            ->setCid($cid)
            ->setMonitorAction('switch',request()->controller().'/change')
            ->setMonitorAction('edit',request()->controller().'/change')
            ->setRightHeader('操作',$width);
        foreach ($toops as $value){
            if($value=='alldel'){
                $this->builder = $this->builder->addToolbarDemo('alldel','全部删除',[],request()->controller().'/del');
            }
            if($value=='add'){
                $this->builder = $this->builder->addToolbarDemo('add','添加',[],request()->controller().'/show_add');
            }
            if($value=='back'){
                $this->builder = $this->builder->addToolbarDemo('back','返回');
            }
            if($value=='text'){
                $this->builder = $this->builder->addToolbarDemo('text',$text);
            }
            if($value=='export'){
                $this->builder = $this->builder->addToolbarDemo('export','导出',[],request()->controller().'/exports?cid='.$cid);
            }
        }
        if($fieldsousuo){
            $this->builder = $this->builder->addToolbarDemo('search','搜索',$fieldsousuo);
        }
        if($timesousuo){
            $this->builder = $this->builder->addToolbarDemo('times','确定',$timesousuo);
        }
        return $this;
    }

    /**添加右侧button
     * @param string $right
     * @return $this
     */
    public function addTableButton($right=''){
        $right = explode(',',$right);
        if(count($right)>3){
            $this->builder->setRightHeader(null,300);
        }
        foreach ($right as $value){
            if($value=='edit'){
                $this->builder = $this->builder->setButton($value,'编辑',request()->controller().'/show_edit');
            }
            if($value=='del'){
                $this->builder = $this->builder->setButton($value,'删除',request()->controller().'/del');
            }
            if($value=='add'){
                $this->builder = $this->builder->setButton($value,'添加',request()->controller().'/show_add');
            }
            if($value=='detail'){
                $this->builder = $this->builder->setButton($value,'查看',request()->controller().'/detail');
            }
            if($value=='a'){
                $this->builder = $this->builder->setButton($value,'编辑字段',request()->controller().'/show_list');
            }
        }
        return $this;
    }

    /**加载表格
     * @return \think\response\View
     */
    public function TabelFetch($table='table'){
        foreach ($this->field as $value){
            if($value['show']){
                if($value['form_type']=='无限联动框'||$value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='单选框'||$value['form_type']=='多选框'||$value['form_type']=='联动单选框'||$value['form_type']=='联动多选框'||$value['form_type']=='联动下拉组'){
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name'],'document/change');
                }
                if($value['form_type']=='时间'){
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $this->builder =  $this->builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $this->builder =  $this->builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $this->builder =  $this->builder->setColumn('switch',$value['field'],$value['field_name'],'document/change',null,'center','',$array);
                }
                if($value['form_type']=='json数组'){
                    $this->builder =  $this->builder->setColumn('input',$value['field'],$value['field_name']);
                }
            }
        }
        return $this->builder->fetch($table);
    }
}